import productItemColumn from '@/components/product-item/components/product-item-column.vue'
import { fetchCategoryProduct } from '@/api/category.js';

export default {
	components: {
		productItemColumn
	},
	data() {
		return {
			headerPosition: "fixed",
			headerTop: "0px",
			filterIndex: 0,
			priceOrder: 0, //0 非价格排序  1 价格从低到高 2价格从高到低
			
			productList: [],
			reqParam: {
				categoryId: 0,
				type: 1,
				pageNum: 0,
				pageSize: 6
			},
			loadingType: 'more', //加载更多状态
		};
	},
	onLoad(options) {
		// #ifdef H5
		this.headerTop = document.getElementsByTagName('uni-page-head')[0].offsetHeight + 'px';
		// #endif
		
		this.reqParam.categoryId = options.categoryId;
		uni.setNavigationBarTitle({
			title: options.categoryName
		})

		this.loadData();
	},
	
	onPageScroll(e) {
		//兼容iOS端下拉时顶部漂移
		if (e.scrollTop >= 0) {
			this.headerPosition = "fixed";
		} else {
			this.headerPosition = "absolute";
		}
	},
	
	//加载更多
	onReachBottom() {
		this.loadData();
	},
	methods: {
		/**
		 * 加载数据，刷新和追加 refresh add
		 */
		async loadData(type = 'add') {
			
			if (type === 'add') {
				if (this.loadingType === 'nomore') {
					return;
				}
				this.loadingType = 'loading';
			}else if (type === 'refresh') {
				this.reqParam.pageNum=0;
				this.productList = [];
				this.loadingType = 'more'
			}else{
				return;
			}
			
			//接口获取数据
			fetchCategoryProduct(this.reqParam).then(response => {
				let pageData = response.data;
				//在原先list中追加数据
				this.productList = this.productList.concat(pageData.content);
				
				if(pageData.last){
					//没有更多了
					this.loadingType = 'nomore';
				}else{
					this.reqParam.pageNum++;
					this.loadingType = 'more';
				}
			})
		},
		
		//筛选点击
		tabClick(index) {
			if (this.filterIndex === index && index !== 2) {
				return;
			}
		
			this.filterIndex = index;
			/**
			 * this.reqParam.type
			 * 类型：1 综合排序（默认） 2 销量排序 3 价格升序 4 价格降序
			 */
			if(index === 0){
				this.reqParam.type = 1;
			}else if(index === 1){
				this.reqParam.type = 2;
			}else if(index === 2){
				/**
				 * this.priceOrder 
				 * 1 价格从低到高 2价格从高到低
				 */
				if(this.priceOrder === 1){
					this.priceOrder = 2;
					this.reqParam.type = 4;
				}else{
					this.priceOrder = 1;
					this.reqParam.type = 3;
				}
			}
			
			uni.pageScrollTo({
				duration: 300,
				scrollTop: 0
			})
			
			this.loadData('refresh');
		},
		
	},
}